package jaoso.framework.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobHandler;

import java.io.InputStream;
import java.io.Reader;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author ��Ե�¿�
 * @since 2004-10-7
 */
public class MyLobHandler implements LobHandler
{
    private final Log log = LogFactory.getLog(getClass());

    private LobHandler defaultLobHandler;

    private LobHandler oracleLobHandler;

    private LobHandler myLobHandler;

    private boolean isOracle;

    private String dataBaseType;

    /**
     *  
     */
    private MyLobHandler()
    {
        super();
    }

    /**
     * @return Returns the isOracle.
     */
    private boolean isOracle()
    {
        return getDataBaseType().indexOf("Oracle") != -1;
    }

    /**
     * @return Returns the oracleLobHandler.
     */
    private LobHandler getOracleLobHandler()
    {
        return oracleLobHandler;
    }

    /**
     * @param oracleLobHandler
     *            The oracleLobHandler to set.
     */
    public void setOracleLobHandler(LobHandler oracleLobHandler)
    {
        this.oracleLobHandler = oracleLobHandler;
    }

    /**
     * (non-Javadoc)
     * 
     * @see org.springframework.jdbc.support.lob.LobHandler#getBlobAsBytes(java.sql.ResultSet,
     *      int)
     */
    public byte[] getBlobAsBytes(ResultSet arg0, int arg1) throws SQLException
    {
        return getMyLobHandler().getBlobAsBytes(arg0, arg1);
    }

    /**
     * (non-Javadoc)
     * 
     * @see org.springframework.jdbc.support.lob.LobHandler#getBlobAsBinaryStream(java.sql.ResultSet,
     *      int)
     */
    public InputStream getBlobAsBinaryStream(ResultSet arg0, int arg1)
            throws SQLException
    {
        return getMyLobHandler().getBlobAsBinaryStream(arg0, arg1);
    }

    /**
     * (non-Javadoc)
     * 
     * @see org.springframework.jdbc.support.lob.LobHandler#getClobAsString(java.sql.ResultSet,
     *      int)
     */
    public String getClobAsString(ResultSet arg0, int arg1) throws SQLException
    {
        return getMyLobHandler().getClobAsString(arg0, arg1);
    }

    /**
     * (non-Javadoc)
     * 
     * @see org.springframework.jdbc.support.lob.LobHandler#getClobAsAsciiStream(java.sql.ResultSet,
     *      int)
     */
    public InputStream getClobAsAsciiStream(ResultSet arg0, int arg1)
            throws SQLException
    {
        return getMyLobHandler().getClobAsAsciiStream(arg0, arg1);
    }

    /**
     * (non-Javadoc)
     * 
     * @see org.springframework.jdbc.support.lob.LobHandler#getClobAsCharacterStream(java.sql.ResultSet,
     *      int)
     */
    public Reader getClobAsCharacterStream(ResultSet arg0, int arg1)
            throws SQLException
    {
        return getMyLobHandler().getClobAsCharacterStream(arg0, arg1);
    }

    /**
     * (non-Javadoc)
     * 
     * @see org.springframework.jdbc.support.lob.LobHandler#getLobCreator()
     */
    public LobCreator getLobCreator()
    {
        return getMyLobHandler().getLobCreator();
    }

    /**
     * @return Returns the myLobHandler.
     */
    private LobHandler getMyLobHandler()
    {
        if (isOracle())
        {
            myLobHandler = getOracleLobHandler();
        } else
        {
            myLobHandler = getDefaultLobHandler();
        }

        return myLobHandler;
    }

    /**
     * @param myLobHandler
     *            The myLobHandler to set.
     */
    private void setMyLobHandler(LobHandler myLobHandler)
    {
        this.myLobHandler = myLobHandler;
    }

    /**
     * @return Returns the defaultLobHandler.
     */
    private LobHandler getDefaultLobHandler()
    {
        return defaultLobHandler;
    }

    /**
     * @param defaultLobHandler
     *            The defaultLobHandler to set.
     */
    public void setDefaultLobHandler(LobHandler defaultLobHandler)
    {
        this.defaultLobHandler = defaultLobHandler;
    }

    /**
     * @return Returns the dataBaseType.
     */
    private String getDataBaseType()
    {
        return dataBaseType;
    }

    /**
     * @param dataBaseType
     *            The dataBaseType to set.
     */
    public void setDataBaseType(String dataBaseType)
    {
        this.dataBaseType = dataBaseType;
    }
}